<template lang="pug">
	scroll-view.my-order-wrap(scroll-y @scrolltolower="scrollToLower")
		no-item(text="暂无相关记录" v-if="noItem")
		.order-item(v-for="(item,index) in list" :key="index" @click="goDetail(item)")
			.item-block
				.item-left
					.hospital-name {{item.hospital_name}}
					.department {{item.department_name}} {{item.doctor_name?item.doctor_name:"普通门诊"}}
				.item-right
					.state(:class="{'active':item.res_type === '5'}") {{item.stateText}}
			.item-block
				.item-left
					.date {{item.yysjd}} {{item.week}} {{item.outTimeText}}
					.name 就诊人 {{item.patient_name}}
				.item-right
					.price {{item.totalPrice}}元
			.item-button-group(v-if="item.res_type === '2' || item.res_type === '5'")
				.item-button(v-if="item.pay_status==='0'" @click.stop="cancel(item)") 退号
				.item-button(v-if="item.res_type === '5' && item.isToday" @click.stop="wait(item)") 排队叫号
</template>

<script>
import noItem from '@/components/no-item'
export default {
	name: 'index',
	data() {
		return {
			userInfo: {},
			noItem: false,
			list: [],
			overFlag: false,
			page: 0,
			size: 10
		}
	},
	created() {},
	mounted() {
		this.userInfo = this.wx.getStorageSync('userInfo')
		this.myOrder()
	},
	methods: {
		cancel(item) {
			let that = this
			this.wx.showModal({
				title: '提示',
				content: '确认申请退号？',
				confirmText: '退号',
				success(res) {
					if (res.confirm) {
						that.cancelOrder(item)
					}
				}
			})
		},
		wait(item) {
			this.api.jtApi
				.checkNumber({
					ORG_CODE: item.hospital_id,
					FYBH: '',
					YYGHWYM: item.yyghwym,
					GHRQ: item.schedule_date,
					GHKSDM: item.department_id,
					GHYSBH2: item.doctor_id,
					MZSJ: item.out_time,
					NUM: item.reg_num,
					GHYZM: item.qhyzm
				})
				.then(data => {
					if (data.ret_code === '0') {
						this.wx.showModal({
							title: '当前叫号:' + data.CUR_NUM,
							content: '您的叫号:' + data.REG_NUM,
							showCancel: false,
							success(res) {}
						})
					} else {
						this.wx.showToast({
							title: data.ret_info,
							icon: 'none'
						})
					}
				})
				.catch(() => {})
		},
		myOrder() {
			this.wx.showLoading({
				title: '加载中',
				icon: 'none'
			})
			this.page++
			let weekArray = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
			this.api.jtApi
				.myOrder({
					access_token: this.userInfo.accessToken,
					member_num: this.userInfo.memberNum,
					res_type: '1',
					page_index: this.page,
					page_size: this.size
				})
				.then(data => {
					this.wx.hideLoading()
					if (data.ret_code === '0') {
						this.overFlag = !data.records
						let list = this.tools.formatToArray(data.records.record)
						let today = this.dayjs()
						list.forEach(item => {
							item.res_type === '2' && (item.stateText = '未完成的预约')
							item.res_type === '3' && (item.stateText = '未成功的预约')
							item.res_type === '4' && (item.stateText = '已退号')
							item.res_type === '5' && (item.stateText = '预约成功')
							item.res_type === '6' && (item.stateText = '预约已过期')

							item.out_time === 'A' && (item.outTimeText = '上午')
							item.out_time === 'P' && (item.outTimeText = '下午')
							item.out_time === 'F' && (item.outTimeText = '全天')
							item.out_time === 'N' && (item.outTimeText = '夜间')

							item.week = weekArray[this.dayjs(item.schedule_date).day()]
							item.isToday = this.dayjs(today.format('YYYY-MM-DD')).isSame(
								this.dayjs(item.schedule_date)
							)

							item.totalPrice = this.filter.formatIncome(
								item.reg_fee * 1.0 + item.clinic_fee * 1.0
							)
						})
						this.list = this.list.concat(list)
					}
					this.noItem = this.list.length <= 0
				})
				.catch(() => {
					this.noItem = this.list.length <= 0
					this.wx.hideLoading()
				})
		},
		cancelOrder(item) {
			this.api.jtApi
				.cancelOrder({
					access_token: this.userInfo.accessToken,
					member_num: this.userInfo.memberNum,
					schedule_date: item.schedule_date,
					schedule_ghxh: item.schedule_ghxh,
					member_num_porxy: item.member_num_proxy
				})
				.then(data => {
					if (data.ret_code === '0') {
						this.wx.showToast({
							title: '退号成功！'
						})
						this.list = []
						this.overFlag = false
						this.page = 0
						this.myOrder()
					} else {
						this.wx.showToast({
							title: data.ret_info,
							icon: 'none'
						})
					}
				})
				.catch(() => {})
		},
		scrollToLower() {
			!this.overFlag && this.myOrder()
		},
		goDetail(item) {
			this.wx.setStorage({
				key: 'myOrderDetail',
				data: item
			})
			this.$router.push({
				path: '/views/myOrder/orderDetail'
			})
		}
	},
	components: {
		noItem
	}
}
</script>

<style lang="stylus">
	.my-order-wrap
		position absolute
		cover()
		.order-item
			margin-top mainMargin
			padding 38px mainPadding 18px
			background-color white
			.item-block
				display flex
				justify-content space-between
				align-items center
				.item-left
					flex 1
				.hospital-name
					font-size 30px
					font-weight 600
					color blackFontColor1A
					line-height 48px
					padding-bottom 8px
				.department
					font-size 28px
					font-weight 500
					color grayFontColor88
					line-height 40px
				.state,.price
					font-size 30px
					font-weight 500
					color grayFontColor88
					line-height 42px
					&.active
						color themeColor
				.date, .name
					font-size 24px
					color grayFontColor88
					line-height 48px
				&:first-child
					padding-bottom 38px
					margin-bottom 18px
					border-bottom borderStyle
			.item-button-group
				display flex
				align-items center
				justify-content flex-end
				.item-button
					width 152px
					height 56px
					border-radius 36px
					border 1px solid grayFontColor88
					line-height 56px
					text-align center
					font-size 26px
					color blackFontColor4C
					margin-left mainMargin
					&.active
						background linear-gradient(90deg,rgba(76,142,255,1) 0%,rgba(54,162,255,1) 100%)
						color white
					&.active, &.done
						border-color themeColor

</style>
